package com.nc.delivery_project.server.service;

import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.OrderWaypointDao;
import com.nc.delivery_project.server.domain.Department;
import com.nc.delivery_project.server.domain.OrderWaypoint;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Natalie D.
 * Date: 2/1/15
 * Time: 4:52 PM
 */
public class OrderWaypointService extends BaseService<OrderWaypoint> {

    private DepartmentService dservice;

    public OrderWaypointService() {
        this(new OrderWaypointDao());
    }

    public OrderWaypointService(Connection connection, boolean isAutoCloseConnection) {
        super(new OrderWaypointDao(), connection, isAutoCloseConnection);
    }

    public OrderWaypointService(AbstractDao<OrderWaypoint> dao) {
        super(dao);
    }


    @Override
    public OrderWaypoint read(int id)
    {
        OrderWaypoint orderWaypoint = null;
        dservice=new DepartmentService();
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            orderWaypoint = dao.read(id);
            if (orderWaypoint != null)
            {
                Department department = dservice.read(orderWaypoint.getDepartment().getDeptId());
                if (department != null)
                {
                    orderWaypoint.setDepartment(department);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return orderWaypoint;
    }

    @Override
    public List<OrderWaypoint> read()
    {
        List<OrderWaypoint> orderWaypoints = null;
        dservice=new DepartmentService();
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            orderWaypoints = dao.read();
            for (OrderWaypoint orderWaypoint : orderWaypoints)
            {
                orderWaypoint.setDepartment(dservice.read(orderWaypoint.getDepartment().getDeptId()));
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return orderWaypoints;
    }

    public List<OrderWaypoint> readByOrderId(int id)
    {
        List<OrderWaypoint> orderWaypoints = null;
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            orderWaypoints = ((OrderWaypointDao)dao).readByOrderId(id);
            dservice = new DepartmentService();

            for (OrderWaypoint orderWaypoint: orderWaypoints)
            {
                orderWaypoint.setDepartment(dservice.read(orderWaypoint.getDepartment().getDeptId()));
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return orderWaypoints;
    }

    public List<OrderWaypoint> readByDepartmentId(int id)
    {
        List<OrderWaypoint> orderWaypoints = null;
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            orderWaypoints = ((OrderWaypointDao)dao).readByDepartmentId(id);
            dservice = new DepartmentService();

            for (OrderWaypoint orderWaypoint: orderWaypoints)
            {
                orderWaypoint.setDepartment(dservice.read(id));
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return orderWaypoints;
    }
}


